package com.blb.zycommunity.controller;

import com.blb.zycommunity.config.AutoLog;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysMenu;
import com.blb.zycommunity.mapper.SysMenuMapper;
import com.blb.zycommunity.service.ISysMenuService;
import com.blb.zycommunity.utils.TreeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
public class SysMenuController {

    static private TreeUtils treeUtils = new TreeUtils();

    @Resource
    private ISysMenuService sysMenuService;

    @Resource
    private SysMenuMapper sysMenuMapper;

    /**
     * 查询一级菜单
     */
    @GetMapping("sysmenu")
    public ResponseResult findSysMenuAndChild(String userName){
        List<SysMenu> sysMenus = sysMenuService.selectSysMenuWithChild(userName);
        return ResponseResult.ok(sysMenus);
    }

    /**
     * 查询多级菜单
     */
    @GetMapping("sysmenu-all")
    public ResponseResult<List<SysMenu>> findSysMenuAll(@RequestParam(required = false, defaultValue = "")String menuName,
                                                        @RequestParam(required = false, defaultValue = "")String status){
        List<SysMenu> sysMenus = sysMenuMapper.selectAllSysMenu(menuName,status);
        List<SysMenu> sysMenus1 = treeUtils.menusToTree(sysMenus);
        return ResponseResult.ok(sysMenus1);
    }

    /**
     * 通过id查寻菜单的全部数据
     */
    @GetMapping("sysmenu-id")
    public ResponseResult<SysMenu> findSysMenuId(Long id){
        SysMenu sysMenu = sysMenuService.getById(id);
        return ResponseResult.ok(sysMenu);
    }

    /**
     * 修改菜单
     */
    @AutoLog(value = 2,title = "菜单管理")
    @PutMapping("sysmenu-update")
    public ResponseResult<String> updateSysMenu(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return ResponseResult.ok("ok");
    }

    /**
     * 添加菜单
     */
    @AutoLog(value = 1,title = "菜单管理")
    @PostMapping("sysmenu-add")
    public ResponseResult<String> addSysMenu(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return ResponseResult.ok("ok");
    }

    /**
     * 删除菜单
     */
    @AutoLog(value = 2,title = "菜单管理")
    @DeleteMapping("/sysmenu-delete/{id}")
    public ResponseResult deleteSysMenu(@PathVariable Long id){
        sysMenuService.removeById(id);
        return ResponseResult.ok("ok");
    }


    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("/SysMenu")
    public ResponseResult filAll(){
        List<SysMenu> sysMenu = sysMenuService.list();
        List<SysMenu> sysMenus = TreeUtils.menusToTree(sysMenu);
        return ResponseResult.ok(sysMenus);
    }






}
